﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufBase.h"
#include "GnufComponent.h"
#include "GnufItemEvents.h"
#include "GnufNotificationCenter.h"
#include "GnufAppearanceSetting.h"
namespace gnuf {

    class QtWindow;
    class Preferences;
    class GNUF_BASE_EXPORT WindowTitleBar final
        : public Component
        , public IItemEventHandler
        , public INotificationCenterEventHandler
    {
        DEFINE_CAST_DERIVED(WindowTitleBar, Component)
    public:
        explicit WindowTitleBar(
            QtWindow *,
            const std::wstring & key, 
            const std::wstring & config,
            bool isRelativePath);
        ~WindowTitleBar();

        static const wchar_t *  defaultKey;
        static const wchar_t *  defaultConfig;
        static const int        defaultFixedHeight;

        static WindowTitleBar * get();

        // The window title bar can couple with an arrangement area that
        // doesn't know which window title bar is coupling with itself,
        // and can still connect with or disconnect from an arrangement
        // area, normally. Subsequently, do something with this coupled
        // arrangement area. See also, mouseClickOnBlank() implementation.
        void   coupleWith(ArrangementArea *);
        void uncoupleFrom(const ArrangementArea * const);
        const ArrangementArea * coupledArrangementArea() const;

        void setWindowTitle(const std::wstring &);
        const std::wstring & windowTitle(const std::wstring &) const;

        void setWindowTitleVisible(const std::wstring &, bool);
        bool isWindowTitleVisible(const std::wstring &) const;

        bool hasProjectDropdown(const std::wstring &) const;
        void setProjectDropdownVisible(const std::wstring &, bool);
        bool isProjectDropdownVisible(const std::wstring &) const;

        void donotWantToSyncWindowTitle() {
            m_wantToSyncWindowTitle = false;
        }

        bool hasNothingAt(int globalX, int globalY);
        void mouseClickOnBlank();
        void updateIconBy(bool maximized);
        
        void respondTo(const Preferences*);

    protected:
        // Inherits from Component...
        virtual void initialize(Toolbar *) override;
        virtual gcmp::OwnerPtr<QtToolbar> createQtToolBar(Toolbar*) override;
            
        // Inherits from IItemEventHandler...
        virtual bool onPreClicked(Item *) override;
        virtual void onClicked(Item *) override;
        virtual void onHovered(Item *, bool hovered, void * qtWidget) override;

        // Inherits from INotificationCenterEventHandler...
        virtual void onEvent(INotificationCenterEventArgs *) override;

        void onButtonAdded(ButtonControl*, const std::wstring&, ButtonEx*) const override;

    private:
        gcmp::OwnerPtr<ButtonAppearanceSetting> createCustomizeBtnAppearanceSetting(const bool);
        void setCustomizeBtnAppearanceSetting(Toolbar* );
        bool coupledArrangementAereIsMasked(bool * pOverall = nullptr) const;
    private:
        QtWindow * m_qtWindow;
        bool m_wantToSyncWindowTitle;
        gcmp::WeakPtr<ArrangementArea> m_wpCoupledArrangementArea;
    };
}
